package com.example.administrator.mvp.module.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.administrator.mvp.R;
import com.example.administrator.mvp.base.BaseActivity;
import com.example.administrator.mvp.model.user.UserModel;
import com.example.administrator.mvp.module.user.UserActivity;

/**
 * 登录页面
 */
public class LoginActivity extends BaseActivity<LoginPresenter> implements LoginContract.View, View.OnClickListener {
    EditText et_username, et_password;
    Button btn_login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        //初始化控件
        et_username = findViewById(R.id.et_username);
        et_password = findViewById(R.id.et_password);
        btn_login = findViewById(R.id.btn_login);
        btn_login.setOnClickListener(this);

        //初始化Presenter
        presenter = new LoginPresenter(this, new UserModel(this));
    }

    @Override
    public void onClick(View v) {
        String user = et_username.getText().toString();
        String password = et_password.getText().toString();
        if (TextUtils.isEmpty(user)) {
            Toast.makeText(this, "用户名不能为空",
                    Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(password)) {
            Toast.makeText(this, "密码不能为空",
                    Toast.LENGTH_SHORT).show();
            return;
        }
        //登录
        presenter.login(user, password);
    }

    /**
     * 登录成功
     */
    @Override
    public void loginSuccess() {
        Intent intent = new Intent(this, UserActivity.class);
        startActivity(intent);
    }

    /**
     * 登录失败
     */
    @Override
    public void loginFailure() {
        Toast.makeText(this, "登录失败", Toast.LENGTH_SHORT).show();
    }
}
